﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using System.ComponentModel.Composition;

namespace Kooboo.Commerce.Repositories
{
    [Export(typeof(ICategoryRepository))]
    public partial class EntityRepositories:ICategoryRepository
    {
        public IQueryable<ICategory> Categories
        {
            get
            {
                return this.DataReaderContext.Categories.OfType<ICategory>();
            }
        }

        public void Post(ICategory category)
        {
            Contract.Requires(category != null);

            var item = new Category();
            item.Name = category.Name;
            if (category.Parent != null)
                item.ParentId = category.Parent.Id;
            else
                item.ParentId = null;
            item.Photo = category.Photo;
            item.Description = category.Description;

            this.DataWriterContext.Categories.InsertOnSubmit(item);
            this.DataWriterContext.SubmitChanges();

        }

        public void Put(ICategory category)
        {
            Contract.Requires(category != null);

            var item = this.DataWriterContext.Categories.Where(i => i.Id == category.Id)
                .FirstOrDefault();

            if (item == null)
            {
                throw new KeyNotFoundException("Category:" + category.Id.ToString());
            }

            item.Name = category.Name;
            item.Photo = category.Photo;
            item.Description = category.Description;

            if (category.Parent != null)
                item.ParentId = category.Parent.Id;
            else
                item.ParentId = null;

            this.DataWriterContext.SubmitChanges();
        }

        public void Delete(ICategory category)
        {
            var item = this.DataWriterContext.Categories
                .Where(i => i.Id == category.Id)
               .FirstOrDefault();

            if (item != null)
            {
                this.DataWriterContext.Categories.DeleteOnSubmit(item);

                var list = this.DataWriterContext.CategoryProducts.Where(i => i.CategoryId == item.Id);

                this.DataWriterContext.CategoryProducts.DeleteAllOnSubmit(list);

                this.DataWriterContext.SubmitChanges();
            }
        }

        public void Include(ICategory category, IProduct product)
        {
            Contract.Requires(category != null);
            Contract.Requires(product != null);

            var item = new CategoryProduct
            {
                CategoryId = category.Id,
                ProductId = product.Id
            };

            this.DataWriterContext.CategoryProducts.InsertOnSubmit(item);

            this.DataWriterContext.SubmitChanges();
        }

        public void Exclude(ICategory category, IProduct product)
        {
            Contract.Requires(category != null);
            Contract.Requires(product != null);

            var item = this.DataWriterContext.CategoryProducts
                .Where(i => i.Category == category && i.Product == product)
                .FirstOrDefault();

            if (item != null)
            {
                this.DataWriterContext.CategoryProducts.DeleteOnSubmit(item);
                this.DataWriterContext.SubmitChanges();
            }
        }
    }
}
